/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.example;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Skeleton for a Flink DataStream Job.
 *
 * <p>For a tutorial how to write a Flink application, check the
 * tutorials and examples on the <a href="https://flink.apache.org">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution, run
 * 'mvn clean package' on the command line.
 *
 * <p>If you change the name of the main class (with the public static void main(String[] args))
 * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
 */
public class DataStreamJob {
	private static final Logger LOG = LoggerFactory.getLogger(DataStreamJob.class);

	static class Test1MapFunction implements MapFunction<String, ArrayList<Point>> {
		@Override
		public ArrayList<Point> map(String s) throws Exception {
			long timeMillis = System.currentTimeMillis();
			var seri = new QTreeSerializer();
			var quadTree = seri.deserializeCustom(s);
			var afg = new QTreeFromGrid();
			afg.computeAllNeighbors(quadTree);
			// 寻路
			var route = new AStar(quadTree).findPath(new Point(110, 338), new Point(484, 372));
            LOG.info("[****]a job mainly work is finished in {} ms![****]", System.currentTimeMillis() - timeMillis);
			return route;
		}
	}

	public static void main(String[] args) throws Exception {

		var g = "hello, world";
		System.out.println(g);
		// Sets up the execution environment, which is the main entry point
		// to building Flink applications.
		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		var files = getStrings();

		DataStreamSource<String> r = env.fromCollection(files);
		var x = r.map(new Test1MapFunction());
		x.print();
		/*
		 * Here, you can start creating your execution plan for Flink.
		 *
		 * Start with getting some data from the environment, like
		 * 	env.fromSequence(1, 10);
		 *
		 * then, transform the resulting DataStream<Long> using operations
		 * like
		 * 	.filter()
		 * 	.flatMap()
		 * 	.window()
		 * 	.process()
		 *
		 * and many more.
		 * Have a look at the programming guide:
		 *
		 * https://nightlies.apache.org/flink/flink-docs-stable/
		 *
		 */

		// Execute program, beginning computation.
		env.execute("Flink Java API Skeleton");
	}

	private static ArrayList<String> getStrings() {
		var files = new ArrayList<String>();
		files.add("/var/flinkrc/serializeBinary0.data");
		files.add("/var/flinkrc/serializeBinary1.data");
		files.add("/var/flinkrc/serializeBinary2.data");
		files.add("/var/flinkrc/serializeBinary3.data");
		files.add("/var/flinkrc/serializeBinary4.data");
		files.add("/var/flinkrc/serializeBinary5.data");
		files.add("/var/flinkrc/serializeBinary6.data");
		files.add("/var/flinkrc/serializeBinary7.data");
		files.add("/var/flinkrc/serializeBinary8.data");
		files.add("/var/flinkrc/serializeBinary9.data");
		files.add("/var/flinkrc/serializeBinary10.data");
		files.add("/var/flinkrc/serializeBinary11.data");
		files.add("/var/flinkrc/serializeBinary12.data");
		files.add("/var/flinkrc/serializeBinary13.data");
		files.add("/var/flinkrc/serializeBinary14.data");
		files.add("/var/flinkrc/serializeBinary15.data");
		files.add("/var/flinkrc/serializeBinary16.data");
		files.add("/var/flinkrc/serializeBinary17.data");
		files.add("/var/flinkrc/serializeBinary18.data");
		files.add("/var/flinkrc/serializeBinary19.data");
		return files;
	}
}
